package com.zeta.eduservice.utils.word;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zeta.eduservice.entity.question.EduQuestionChoice;
import com.zeta.eduservice.entity.question.EduQuestionTypeSys;
import com.zeta.eduservice.entity.vo.CreatePaperType;
import com.zeta.eduservice.entity.vo.PaperAndQuestionTypeSysVo;
import com.zeta.eduservice.entity.vo.PaperListVo;
import com.zeta.eduservice.entity.vo.QuestionChoiceVo;
import com.zeta.eduservice.entity.vo.QuestionQuery;
import com.zeta.eduservice.entity.vo.questionVo;
import com.zeta.eduservice.entity.vo.front.ChapterCourseDetialVo;
import com.zeta.eduservice.entity.vo.front.PdfQuestionVo;
import com.zeta.eduservice.entity.vo.front.QuestionCreateInfo;
import com.zeta.eduservice.entity.vo.front.front.PaperInfoVo;
import com.zeta.eduservice.entity.yml.TeacherFileYml;
import com.zeta.eduservice.mapper.EduQuestionChoiceMapper;
import com.zeta.eduservice.mapper.EduQuestionMapper;
import com.zeta.eduservice.service.EduPaperService;
import com.zeta.eduservice.service.EduQuestionChoiceService;
import com.zeta.eduservice.service.EduQuestionService;
import com.zeta.eduservice.service.EduQuestionTypeSysService;
import com.zeta.exceptionhandler.GuliException;
@Component
public class Wordload extends Load{
	  @Autowired
	    private TeacherFileYml teacherFileYml;
	@Autowired
	private EduQuestionService questionService;
	@Autowired
	private EduQuestionChoiceService choiceService;
	@Autowired
	private EduPaperService paperService;
	
	@Autowired
	WordCreateUtils wordUtils;
	@Autowired
	EduQuestionTypeSysService eduQuestiontypeService;

	@Autowired
	EduQuestionChoiceMapper questionChoiceMapper;
	@Autowired
	EduQuestionMapper questionMapper;
//	public void CreatePaper(String path, List<QuestionCreateInfo> list, PaperInfoVo paperInfo,
//			List<ChapterCourseDetialVo> chapterList, Integer choice) throws IOException, InvalidFormatException {
//		XWPFDocument document = new XWPFDocument();
//	
//		FileOutputStream out = new FileOutputStream(new File(path));
//
//		XWPFParagraph p1 = document.createParagraph();
////			p1.setAlignment(ParagraphAlignment.CENTER);
//		XWPFRun r1 = p1.createRun();
//		r1.setBold(true);
//		r1.setText(paperInfo.getPapertitle());
//		r1.setFontFamily("黑体");
//		r1.setFontSize(18);
//		r1.addBreak();
//
//		XWPFRun r2 = p1.createRun();
//		r2.setBold(true);
//		r2.setText(paperInfo.getTesttitle());
//		r2.setFontFamily("黑体");
//		r2.setFontSize(16);
//		r2.addBreak();
//
//		XWPFRun r3 = p1.createRun();
//		r3.setBold(true);
//		r3.setText(paperInfo.getClasstitle());
//		r3.setFontFamily("黑体");
//		r3.setFontSize(14);
//		r3.addBreak();
//		r3.setTextPosition(0);
//		String[] s = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四" };
//		XWPFTable table = document.createTable(2, list.size() + 2);
//		table.createRow();
//		List<XWPFTableRow> rows = table.getRows();
//		// 表格属性
//		CTTblPr tablePr = table.getCTTbl().addNewTblPr();
//		// 表格宽度
//		CTTblWidth width = tablePr.addNewTblW();
//		width.setW(BigInteger.valueOf(8000));
//		XWPFTableRow row;
//		List<XWPFTableCell> cells;
//		XWPFTableCell cell;
//		int rowSize = rows.size();
//		int cellSize;
//		int n = 0;
//
//		for (int i = 0; i < rowSize; i++) {
//			row = rows.get(i);
//			// 新增单元格
//			row.addNewTableCell();
//			// 设置行的高度
//			row.setHeight(500);
//			// 行属性
////	       CTTrPr rowPr = row.getCtRow().addNewTrPr();  
//			// 这种方式是可以获取到新增的cell的。
////	       List<CTTc> list = row.getCtRow().getTcList();  
//			cells = row.getTableCells();
//			cellSize = cells.size();
//			for (int j = 0; j < cellSize; j++) {
//				cell = cells.get(j);
////	                cell.setColor("ff0000"); //红色  
//				// 单元格属性
//				if (i == 0) {
//					if (j == 0)
//						cell.setText("题 号");
//					else if (j > 0 && j < cellSize - 2) {
//						cell.setText(s[n++]);
//					} else if (j == cellSize - 2) {
//						cell.setText("总 分");
//					} else if (j == cellSize - 1) {
//						cell.setText("复核人");
//					}
//				} else if (i == 1) {
//					if (j == 0) {
//						cell.setText("得 分");
//					}
//				} else if (i == 2) {
//					if (j == 0) {
//						cell.setText("阅卷人");
//					}
//				}
//				CTTc cttc = cell.getCTTc();
//				CTTcPr ctPr = cttc.addNewTcPr();
//				ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
//				ctPr.addNewTcW().setW(BigInteger.valueOf(1100));
//				cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
//			}
//		}
//		WordCreateUtils.mergeCellsVertically(table, list.size() + 2, 1, 2);
//
//		XWPFParagraph w = document.createParagraph();
//		XWPFRun we = w.createRun();
//		we.setText("    ");
//
//		int n2 = 0;
//		XWPFTableRow RowCurr;
//		for (QuestionCreateInfo i : list) {
//			if (i.getType().equals("选择")) {
//				QueryWrapper<EduQuestionTypeSys> queryWrapper = new QueryWrapper<EduQuestionTypeSys>();
//				queryWrapper.eq("type", "选择");
//				EduQuestionTypeSys obj = eduQuestiontypeService.getOne(queryWrapper);
//				XWPFTable t1 = document.createTable(2, 3);
//				List<XWPFTableRow> rows2 = t1.getRows();
//				for (int j3 = 0; j3 < rows2.size(); j3++) {
//					RowCurr = rows2.get(j3);
//					if (j3 == 0)
//						RowCurr.setHeight(200);
//					if (j3 == 1)
//						RowCurr.setHeight(600);
//					List<XWPFTableCell> tableCells = RowCurr.getTableCells();
//					for (int j2 = 0; j2 < tableCells.size(); j2++) {
//						XWPFTableCell cellt = tableCells.get(j2);
//						if (j2 == 0) {
//							if (j3 == 0)
//								cellt.setText("得 分");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
//							ctPr.addNewTcW().setW(BigInteger.valueOf(900));
//							cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
//						} else if (j2 == 1) {
//							cellt.setText("");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
//							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
//							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
//							ctPr.addNewTcW().setW(BigInteger.valueOf(300));
//						} else if (j2 == 2) {
//							if (j3 == 0) {
//								if (choice == 0) {
//									cellt.setText(s[n2++] + "、" + obj.getName() + "（每小题" + i.getScore() + "分，共"
//											+ i.getNumber() * i.getScore() + "分）");
//								} else if (choice == 1) {
//									String ss = s[n2++] + "、" + obj.getName() + "（";
//									String midss = "";
//									int nums = 0;
//									for (int ii = 0; ii < chapterList.size(); ii++) {
//										ChapterCourseDetialVo chapterCourseDetialVo = chapterList.get(ii);
//										Integer number = chapterCourseDetialVo.getNumber();
//										Integer score = chapterCourseDetialVo.getScore();
//										midss = midss + (ii + 1) + "-" + (ii + 1 + number) + "小题,每题" + score + "分,";
//										nums += number * score;
//									}
//									cellt.setText(ss + midss + "共" + nums + "分）");
//								}
//							}
//
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							ctPr.addNewTcW().setW(BigInteger.valueOf(7000));
//							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
//							addNewTcBorders.addNewLeft().setVal(STBorder.NIL);
//							addNewTcBorders.addNewRight().setVal(STBorder.NIL);
//							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
//							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
//						}
//					}
//				}
//				WordCreateUtils.mergeCellsVertically(t1, 1, 0, 1);
//				WordCreateUtils.mergeCellsVertically(t1, 2, 0, 1);
////列合并参数：表格，行，开始列，结束列
//
//				XWPFParagraph createParagraph = document.createParagraph();
//				XWPFRun createRun = createParagraph.createRun();
//				createRun.setText("    ");
//
//				wordUtils.choiceTable(document, i, i.getNumber());
//
//				XWPFParagraph p2 = document.createParagraph();
//				p2.setSpacingBefore(200);
//				List<EduQuestionChoice> rand = new ArrayList<EduQuestionChoice>();
//				if (choice == 0) {
//					rand = getQuestionByChoice(i.getDifficulty(), i.getNumber());// 全部随机模式
//
//				} else if (choice == 1) {
//					rand = getQuestionByChoice(i.getDifficulty(), i.getNumber(), chapterList, paperInfo);// 按章节 控制题数 模式
//				}
//				int num = 1;
//				XWPFRun xr2 = p2.createRun();
//				xr2.setFontFamily("黑体");
//				xr2.setFontSize(12);
//				for (EduQuestionChoice j : rand) {
//					xr2.setText(num++ + "." + j.getContent());
//					xr2.addBreak();
//					xr2.setText("A." + j.getChoiceA());
//					xr2.addBreak();
//					xr2.setText("B." + j.getChoiceB());
//					xr2.addBreak();
//					xr2.setText("C." + j.getChoiceC());
//					xr2.addBreak();
//					xr2.setText("D." + j.getChoiceD());
//					xr2.addBreak();
//				}
//			} else {
//				QueryWrapper<EduQuestionTypeSys> queryWrapper = new QueryWrapper<EduQuestionTypeSys>();
//				queryWrapper.eq("type", i.getType());
//				EduQuestionTypeSys obj = eduQuestiontypeService.getOne(queryWrapper);
//				XWPFTable t1 = document.createTable(2, 3);
//				List<XWPFTableRow> rows2 = t1.getRows();
//				for (int j3 = 0; j3 < rows2.size(); j3++) {
//					RowCurr = rows2.get(j3);
//					if (j3 == 0)
//						RowCurr.setHeight(200);
//					if (j3 == 1)
//						RowCurr.setHeight(600);
//					List<XWPFTableCell> tableCells = RowCurr.getTableCells();
//					for (int j2 = 0; j2 < tableCells.size(); j2++) {
//						XWPFTableCell cellt = tableCells.get(j2);
//						if (j2 == 0) {
//							if (j3 == 0)
//								cellt.setText("得 分");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
//							ctPr.addNewTcW().setW(BigInteger.valueOf(900));
//							cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
//						} else if (j2 == 1) {
//							cellt.setText("");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
//							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
//							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
//							ctPr.addNewTcW().setW(BigInteger.valueOf(300));
//						} else if (j2 == 2) {
//							if (j3 == 0)
//								cellt.setText(s[n2++] + "、" + obj.getName() + "（每小题" + i.getScore() + "分，共"
//										+ i.getNumber() * i.getScore() + "分）");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							ctPr.addNewTcW().setW(BigInteger.valueOf(7000));
//							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
//							addNewTcBorders.addNewLeft().setVal(STBorder.NIL);
//							addNewTcBorders.addNewRight().setVal(STBorder.NIL);
//							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
//							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
//						}
//					}
//				}
//				XWPFParagraph p2 = document.createParagraph();
//				p2.setSpacingBefore(200);
//				XWPFRun xr2 = p2.createRun();
//				xr2.setFontFamily("黑体");
//				xr2.setFontSize(12);
//				List<PdfQuestionVo> byRand = new ArrayList<PdfQuestionVo>();
//				if (choice == 0)
//					byRand = getQuestionByType(i.getType(), i.getDifficulty(), i.getNumber());
//				else if (choice == 1)
//					byRand = getQuestionByType(i.getType(), i.getDifficulty(), i.getNumber(), chapterList, paperInfo);
//
//				int num = 1;
//				for (PdfQuestionVo j : byRand) {
//					xr2.setText(num++ + "." + j.getContent());
//					xr2.addBreak();
//				}
//			}
//		}
//		document.write(out);
//		out.close();
//	}
//
//
//	public void CreatePaper2(String path, List<QuestionCreateInfo> list, PaperInfoVo paperInfo,
//			List<ChapterCourseDetialVo> chapterList, Integer choice) throws IOException, InvalidFormatException {
//		
//		InputStream is = new FileInputStream(destPath);
//		XWPFDocument document = new XWPFDocument(is);
//////////////////////
//		XWPFParagraph p1 = document.createParagraph();
//		p1.setAlignment(ParagraphAlignment.CENTER);
//		XWPFRun r1 = p1.createRun();
//		r1.setBold(true);
//		r1.setText(paperInfo.getPapertitle());
//		r1.setFontFamily("黑体");
//		r1.setFontSize(18);
//		r1.addBreak();
//
//		XWPFRun r2 = p1.createRun();
//		r2.setBold(true);
//		r2.setText(paperInfo.getTesttitle());
//		r2.setFontFamily("黑体");
//		r2.setFontSize(16);
//		r2.addBreak();
//
//		XWPFRun r3 = p1.createRun();
//		r3.setBold(true);
//		r3.setText(paperInfo.getClasstitle());
//		r3.setFontFamily("黑体");
//		r3.setFontSize(14);
//		r3.addBreak();
//		r3.setTextPosition(0);
//		String[] s = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四" };
//		XWPFTable table = document.createTable(2, list.size() + 2);
//		table.createRow();
//		List<XWPFTableRow> rows = table.getRows();
//		// 表格属性
//		CTTblPr tablePr = table.getCTTbl().addNewTblPr();
//		// 表格宽度
//		CTTblWidth width = tablePr.addNewTblW();
//		width.setW(BigInteger.valueOf(8000));
//		XWPFTableRow row;
//		List<XWPFTableCell> cells;
//		XWPFTableCell cell;
//		int rowSize = rows.size();
//		int cellSize;
//		int n = 0;
//
//		for (int i = 0; i < rowSize; i++) {
//			row = rows.get(i);
//			// 新增单元格
//			row.addNewTableCell();
//			// 设置行的高度
//			row.setHeight(500);
//			// 行属性
////	       CTTrPr rowPr = row.getCtRow().addNewTrPr();  
//			// 这种方式是可以获取到新增的cell的。
////	       List<CTTc> list = row.getCtRow().getTcList();  
//			cells = row.getTableCells();
//			cellSize = cells.size();
//			for (int j = 0; j < cellSize; j++) {
//				cell = cells.get(j);
////	                cell.setColor("ff0000"); //红色  
//				// 单元格属性
//				if (i == 0) {
//					if (j == 0)
//						cell.setText("题 号");
//					else if (j > 0 && j < cellSize - 2) {
//						cell.setText(s[n++]);
//					} else if (j == cellSize - 2) {
//						cell.setText("总 分");
//					} else if (j == cellSize - 1) {
//						cell.setText("复核人");
//					}
//				} else if (i == 1) {
//					if (j == 0) {
//						cell.setText("得 分");
//					}
//				} else if (i == 2) {
//					if (j == 0) {
//						cell.setText("阅卷人");
//					}
//				}
//				CTTc cttc = cell.getCTTc();
//				CTTcPr ctPr = cttc.addNewTcPr();
//				ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
//				ctPr.addNewTcW().setW(BigInteger.valueOf(1100));
//				//cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
//			}
//		}
//		WordCreateUtils.mergeCellsVertically(table, list.size() + 2, 1, 2);
//
//		XWPFParagraph w = document.createParagraph();
//		XWPFRun we = w.createRun();
//		we.setText("    ");
//
//		int n2 = 0;
//		XWPFTableRow RowCurr;
//		for (QuestionCreateInfo i : list) {
//			if (i.getType().equals("选择")) {
//				QueryWrapper<EduQuestionTypeSys> queryWrapper = new QueryWrapper<EduQuestionTypeSys>();
//				queryWrapper.eq("type", "选择");
//				EduQuestionTypeSys obj = eduQuestiontypeService.getOne(queryWrapper);
//				XWPFTable t1 = document.createTable(2, 3);
//				List<XWPFTableRow> rows2 = t1.getRows();
//				for (int j3 = 0; j3 < rows2.size(); j3++) {
//					RowCurr = rows2.get(j3);
//					if (j3 == 0)
//						RowCurr.setHeight(200);
//					if (j3 == 1)
//						RowCurr.setHeight(600);
//					List<XWPFTableCell> tableCells = RowCurr.getTableCells();
//					for (int j2 = 0; j2 < tableCells.size(); j2++) {
//						XWPFTableCell cellt = tableCells.get(j2);
//						if (j2 == 0) {
//							if (j3 == 0)
//								cellt.setText("得 分");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
//							ctPr.addNewTcW().setW(BigInteger.valueOf(900));
//							//cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
//						} else if (j2 == 1) {
//							cellt.setText("");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
//							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
//							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
//							ctPr.addNewTcW().setW(BigInteger.valueOf(300));
//						} else if (j2 == 2) {
//							if (j3 == 0) {
//								if (choice == 0) {
//									cellt.setText(s[n2++] + "、" + obj.getName() + "（每小题" + i.getScore() + "分，共"
//											+ i.getNumber() * i.getScore() + "分）");
//								} else if (choice == 1) {
//									String ss = s[n2++] + "、" + obj.getName() + "（";
//									String midss = "";
//									int nums = 0;
//									for (int ii = 0; ii < chapterList.size(); ii++) {
//										ChapterCourseDetialVo chapterCourseDetialVo = chapterList.get(ii);
//										Integer number = chapterCourseDetialVo.getNumber();
//										Integer score = chapterCourseDetialVo.getScore();
//										midss = midss + (ii + 1) + "-" + (ii + 1 + number) + "小题,每题" + score + "分,";
//										nums += number * score;
//									}
//									cellt.setText(ss + midss + "共" + nums + "分）");
//								}
//							}
//
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							ctPr.addNewTcW().setW(BigInteger.valueOf(7000));
//							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
//							addNewTcBorders.addNewLeft().setVal(STBorder.NIL);
//							addNewTcBorders.addNewRight().setVal(STBorder.NIL);
//							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
//							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
//						}
//					}
//				}
//				WordCreateUtils.mergeCellsVertically(t1, 1, 0, 1);
//				WordCreateUtils.mergeCellsVertically(t1, 2, 0, 1);
////列合并参数：表格，行，开始列，结束列
//
//				XWPFParagraph createParagraph = document.createParagraph();
//				XWPFRun createRun = createParagraph.createRun();
//				createRun.setText("    ");
//
//				wordUtils.choiceTable(document, i, i.getNumber());
//
//				XWPFParagraph p2 = document.createParagraph();
//				p2.setSpacingBefore(200);
//				List<EduQuestionChoice> rand = new ArrayList<EduQuestionChoice>();
//			//////////////////////////////////获取题目
//				if (choice == 0) {
//					rand = getQuestionByChoice(i.getDifficulty(), i.getNumber());// 全部随机模式
//
//				} else if (choice == 1) {
//					rand = getQuestionByChoice(i.getDifficulty(), i.getNumber(), chapterList, paperInfo);// 按章节 控制题数 模式
//				}
//				///////////////////////////
//				int num = 1;
//				XWPFRun xr2 = p2.createRun();
//				xr2.setFontFamily("黑体");
//				xr2.setFontSize(12);
//				for (EduQuestionChoice j : rand) {
//					xr2.setText(num++ + "." + j.getContent());
//					xr2.addBreak();
//					if(j.getImagepath()!=null){
//						XWPFPicture picture = xr2.addPicture(new FileInputStream(j.getImagepath()), Document.PICTURE_TYPE_BMP, j.getImagepath(), 400*360*10,400*360*10);
//						}
//					xr2.addBreak();
//					xr2.setText("A." + j.getChoiceA());
//					xr2.addBreak();
//					xr2.setText("B." + j.getChoiceB());
//					xr2.addBreak();
//					xr2.setText("C." + j.getChoiceC());
//					xr2.addBreak();
//					xr2.setText("D." + j.getChoiceD());
//					xr2.addBreak();
//				}
//			} else {
//				QueryWrapper<EduQuestionTypeSys> queryWrapper = new QueryWrapper<EduQuestionTypeSys>();
//				queryWrapper.eq("type", i.getType());
//				EduQuestionTypeSys obj = eduQuestiontypeService.getOne(queryWrapper);
//				XWPFTable t1 = document.createTable(2, 3);
//				List<XWPFTableRow> rows2 = t1.getRows();
//				for (int j3 = 0; j3 < rows2.size(); j3++) {
//					RowCurr = rows2.get(j3);
//					if (j3 == 0)
//						RowCurr.setHeight(200);
//					if (j3 == 1)
//						RowCurr.setHeight(600);
//					List<XWPFTableCell> tableCells = RowCurr.getTableCells();
//					for (int j2 = 0; j2 < tableCells.size(); j2++) {
//						XWPFTableCell cellt = tableCells.get(j2);
//						if (j2 == 0) {
//							if (j3 == 0)
//								cellt.setText("得 分");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
//							ctPr.addNewTcW().setW(BigInteger.valueOf(900));
//							//cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
//						} else if (j2 == 1) {
//							cellt.setText("");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
//							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
//							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
//							ctPr.addNewTcW().setW(BigInteger.valueOf(300));
//						} else if (j2 == 2) {
//							if (j3 == 0)
//								cellt.setText(s[n2++] + "、" + obj.getName() + "（每小题" + i.getScore() + "分，共"
//										+ i.getNumber() * i.getScore() + "分）");
//							CTTc cttc = cellt.getCTTc();
//							CTTcPr ctPr = cttc.addNewTcPr();
//							ctPr.addNewTcW().setW(BigInteger.valueOf(7000));
//							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
//							addNewTcBorders.addNewLeft().setVal(STBorder.NIL);
//							addNewTcBorders.addNewRight().setVal(STBorder.NIL);
//							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
//							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
//						}
//					}
//				}
//				XWPFParagraph p2 = document.createParagraph();
//				p2.setSpacingBefore(200);
//				XWPFRun xr2 = p2.createRun();
//				xr2.setFontFamily("黑体");
//				xr2.setFontSize(12);
//				List<PdfQuestionVo> byRand = new ArrayList<PdfQuestionVo>();
//				//////////////////////////////获取题目
//				if (choice == 0)
//					byRand = getQuestionByType(i.getType(), i.getDifficulty(), i.getNumber());
//				else if (choice == 1)
//					byRand = getQuestionByType(i.getType(), i.getDifficulty(), i.getNumber(), chapterList, paperInfo);
//////////////////////////
//				int num = 1;
//				for (PdfQuestionVo j : byRand) {
//					xr2.setText(num++ + "." + j.getContent());
//					xr2.addBreak();
//					if(j.getImagepath()!=null){
//						XWPFPicture picture = xr2.addPicture(new FileInputStream(j.getImagepath()), Document.PICTURE_TYPE_BMP, j.getImagepath(), 400*360*10,400*360*10);
//						}
//					xr2.addBreak();
//				}
//			}
//		}
//
//	///////////////////////////
//				OutputStream os = new FileOutputStream(path);
//				document.write(os);
//				os.flush();
//				os.close();
//		
//
//	}
//	////////////////////////
	public void CreatePaperWord(String path, PaperInfoVo paperInfo,CreatePaperType createPaperType) throws IOException, InvalidFormatException {
		PaperListVo paper = paperService.getPaperByPaperId(paperInfo.getPaperId());
		List<PaperAndQuestionTypeSysVo> children = paper.getChildren();
		QuestionQuery searcherObj=new QuestionQuery();
		InputStream is = new FileInputStream(teacherFileYml.getPublicword());
		XWPFDocument document = new XWPFDocument(is);
////////////////////
		XWPFParagraph p1 = document.createParagraph();
		p1.setAlignment(ParagraphAlignment.CENTER);
		XWPFRun r1 = p1.createRun();
		r1.setBold(true);
		r1.setText(paperInfo.getPapertitle());
		r1.setFontFamily("黑体");
		r1.setFontSize(18);
		r1.addBreak();

		XWPFRun r2 = p1.createRun();
		r2.setBold(true);
		r2.setText(paperInfo.getTesttitle());
		r2.setFontFamily("黑体");
		r2.setFontSize(16);
		r2.addBreak();

		XWPFRun r3 = p1.createRun();
		r3.setBold(true);
		r3.setText(paperInfo.getClasstitle());
		r3.setFontFamily("黑体");
		r3.setFontSize(14);
		r3.addBreak();
		r3.setTextPosition(0);
		String[] s = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二", "十三", "十四" };
		if((children.size()+2)>14) {
			throw new GuliException(20001,"题类长度超过14 请联系开发人员更改需求 ");
		}
		XWPFTable table = document.createTable(2, children.size() + 2);
		table.createRow();
		List<XWPFTableRow> rows = table.getRows();
		// 表格属性
		CTTblPr tablePr = table.getCTTbl().addNewTblPr();
		// 表格宽度
		CTTblWidth width = tablePr.addNewTblW();
		width.setW(BigInteger.valueOf(8000));
		XWPFTableRow row;
		List<XWPFTableCell> cells;
		XWPFTableCell cell;
		int rowSize = rows.size();
		int cellSize;
		int n = 0;

		for (int i = 0; i < rowSize; i++) {
			row = rows.get(i);
			// 新增单元格
			row.addNewTableCell();
			// 设置行的高度
			row.setHeight(500);
			// 行属性
//	       CTTrPr rowPr = row.getCtRow().addNewTrPr();  
			// 这种方式是可以获取到新增的cell的。
//	       List<CTTc> list = row.getCtRow().getTcList();  
			cells = row.getTableCells();
			cellSize = cells.size();
			for (int j = 0; j < cellSize; j++) {
				cell = cells.get(j);
//	                cell.setColor("ff0000"); //红色  
				// 单元格属性
				if (i == 0) {
					if (j == 0)
						cell.setText("题 号");
					else if (j > 0 && j < cellSize - 2) {
						cell.setText(s[n++]);
					} else if (j == cellSize - 2) {
						cell.setText("总 分");
					} else if (j == cellSize - 1) {
						cell.setText("复核人");
					}
				} else if (i == 1) {
					if (j == 0) {
						cell.setText("得 分");
					}
				} else if (i == 2) {
					if (j == 0) {
						cell.setText("阅卷人");
					}
				}
				CTTc cttc = cell.getCTTc();
				CTTcPr ctPr = cttc.addNewTcPr();
				ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
				ctPr.addNewTcW().setW(BigInteger.valueOf(1100));
				//cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
			}
		}
		WordCreateUtils.mergeCellsVertically(table, children.size() + 2, 1, 2);

		XWPFParagraph w = document.createParagraph();
		XWPFRun we = w.createRun();
		we.setText("    ");

		int n2 = 0;
		XWPFTableRow RowCurr;
		Integer status = createPaperType.getStatus();
		for (PaperAndQuestionTypeSysVo i : children) {
		
		
			if (i.getQuestionTypeSysId().equals("1")) {
			
				XWPFTable t1 = document.createTable(2, 3);
				List<XWPFTableRow> rows2 = t1.getRows();
				for (int j3 = 0; j3 < rows2.size(); j3++) {
					RowCurr = rows2.get(j3);
					if (j3 == 0)
						RowCurr.setHeight(200);
					if (j3 == 1)
						RowCurr.setHeight(600);
					List<XWPFTableCell> tableCells = RowCurr.getTableCells();
					for (int j2 = 0; j2 < tableCells.size(); j2++) {
						XWPFTableCell cellt = tableCells.get(j2);
						if (j2 == 0) {
							if (j3 == 0)
								cellt.setText("得 分");
							CTTc cttc = cellt.getCTTc();
							CTTcPr ctPr = cttc.addNewTcPr();
							ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
							ctPr.addNewTcW().setW(BigInteger.valueOf(900));
							//cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
						} else if (j2 == 1) {
							cellt.setText("");
							CTTc cttc = cellt.getCTTc();
							CTTcPr ctPr = cttc.addNewTcPr();
							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
							ctPr.addNewTcW().setW(BigInteger.valueOf(300));
						} else if (j2 == 2) {
							if (j3 == 0) {
									cellt.setText(s[n2++] + "、" +i.getName()  + "（每小题" + i.getScore()/i.getNumber() + "分，共"
											+  i.getScore() + "分）");
							}
							CTTc cttc = cellt.getCTTc();
							CTTcPr ctPr = cttc.addNewTcPr();
							ctPr.addNewTcW().setW(BigInteger.valueOf(7000));
							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
							addNewTcBorders.addNewLeft().setVal(STBorder.NIL);
							addNewTcBorders.addNewRight().setVal(STBorder.NIL);
							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
						}
					}
				}
				WordCreateUtils.mergeCellsVertically(t1, 1, 0, 1);
				WordCreateUtils.mergeCellsVertically(t1, 2, 0, 1);
//列合并参数：表格，行，开始列，结束列
				XWPFParagraph createParagraph = document.createParagraph();
				XWPFRun createRun = createParagraph.createRun();
				createRun.setText("    ");
				wordUtils.choiceTable(document,  i.getNumber());
				XWPFParagraph p2 = document.createParagraph();
				p2.setSpacingBefore(200);
				//
				List<QuestionChoiceVo> rand =new ArrayList<QuestionChoiceVo>();
				
				if(status==1) {
					
				}else if(status==2) {
					searcherObj.setTypeId(i.getId());	
					rand = choiceService.getByPaperTypeId(searcherObj);
				}
			
				//
				int num = 1;
				XWPFRun xr2 = p2.createRun();
				xr2.setFontFamily("黑体");
				xr2.setFontSize(12);
				for (QuestionChoiceVo j : rand) {
					xr2.setText(num++ + "." + j.getContent());
					xr2.addBreak();
					if(j.getImagepath()!=null){
						//XWPFPicture picture = xr2.addPicture(new FileInputStream(j.getImagepath()), Document.PICTURE_TYPE_BMP, j.getImagepath(), 400*360*10,400*360*10);
						}
					xr2.addBreak();
					xr2.setText("A." + j.getChoiceA());
					xr2.addBreak();
					xr2.setText("B." + j.getChoiceB());
					xr2.addBreak();
					xr2.setText("C." + j.getChoiceC());
					xr2.addBreak();
					xr2.setText("D." + j.getChoiceD());
					xr2.addBreak();
				}
			} else {
				
				XWPFTable t1 = document.createTable(2, 3);
				List<XWPFTableRow> rows2 = t1.getRows();
				for (int j3 = 0; j3 < rows2.size(); j3++) {
					RowCurr = rows2.get(j3);
					if (j3 == 0)
						RowCurr.setHeight(200);
					if (j3 == 1)
						RowCurr.setHeight(600);
					List<XWPFTableCell> tableCells = RowCurr.getTableCells();
					for (int j2 = 0; j2 < tableCells.size(); j2++) {
						XWPFTableCell cellt = tableCells.get(j2);
						if (j2 == 0) {
							if (j3 == 0)
								cellt.setText("得 分");
							CTTc cttc = cellt.getCTTc();
							CTTcPr ctPr = cttc.addNewTcPr();
							ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
							ctPr.addNewTcW().setW(BigInteger.valueOf(900));
							//cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
						} else if (j2 == 1) {
							cellt.setText("");
							CTTc cttc = cellt.getCTTc();
							CTTcPr ctPr = cttc.addNewTcPr();
							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
							ctPr.addNewTcW().setW(BigInteger.valueOf(300));
						} else if (j2 == 2) {
							if (j3 == 0)
								cellt.setText(s[n2++] + "、" + i.getName() + "（每小题" + i.getScore() + "分，共"
										+ i.getNumber() * i.getScore() + "分）");
							CTTc cttc = cellt.getCTTc();
							CTTcPr ctPr = cttc.addNewTcPr();
							ctPr.addNewTcW().setW(BigInteger.valueOf(7000));
							CTTcBorders addNewTcBorders = cttc.getTcPr().addNewTcBorders();
							addNewTcBorders.addNewLeft().setVal(STBorder.NIL);
							addNewTcBorders.addNewRight().setVal(STBorder.NIL);
							addNewTcBorders.addNewTop().setVal(STBorder.NIL);
							addNewTcBorders.addNewBottom().setVal(STBorder.NIL);
						}
					}
				}
				XWPFParagraph p2 = document.createParagraph();
				p2.setSpacingBefore(200);
				XWPFRun xr2 = p2.createRun();
				xr2.setFontFamily("黑体");
				xr2.setFontSize(12);
			//
				List<questionVo> byPaperTypeId=new ArrayList<questionVo>();
				if(status==1) {
					
				}else if(status==2) {
					searcherObj.setTypeId(i.getId());
					byPaperTypeId = questionService.getByPaperTypeId(searcherObj);
				
				}
				//
				int num = 1;
				for (questionVo j : byPaperTypeId) {
					xr2.setText(num++ + "." + j.getContent());
					xr2.addBreak();
//					if(j.getImagepath()!=null){
//						XWPFPicture picture = xr2.addPicture(new FileInputStream(j.getImagepath()), Document.PICTURE_TYPE_BMP, j.getImagepath(), 400*360*10,400*360*10);
//						}
					xr2.addBreak();
				}
			}
		}

	///////////////////////////
				OutputStream os = new FileOutputStream(path);
				document.write(os);
				os.flush();
				os.close();
		

	}
}
